/* eslint-disable */
// https://cli.vuejs.org/zh/config

// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path')
const fs = require("fs")
// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, on Mac: sudo npm run / sudo yarn
const devServerPort = 9527 // TODO: get this variable from setting.ts
const mockServerPort = 9528 // TODO: get this variable from setting.ts
const name = 'Vue Typescript Admin' // TODO: get this variable from setting.ts

let projectName={}
if (fs.existsSync("./build/project.js")) {
  projectName = require("./build/project")
}else{
  projectName.name ='base'
}
console.log(`##Project: ${projectName.name}`)

module.exports = {
  publicPath:
    process.env.NODE_ENV === 'production'
      ? '/vue-typescript-admin-template/'
      : '/',
  pages: {
    index: {
      entry: projectName.name === 'base' ? "src/main.ts" : `src/projects/${projectName.name}/main.js`
    }
  },
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: devServerPort,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    progress: true,
    proxy: {
      // change xxx-api/login => /mock-api/v1/login
      // detail: https://cli.vuejs.org/config/#devserver-proxy
      [process.env.VUE_APP_BASE_API]: {
        target: 'http://localhost:7300/mock/5f1100217e09f702c0dbd276/vueapi',
        changeOrigin: true, // needed for virtual hosted sites
        ws: true, // proxy websockets
        pathRewrite: {
          ['^' + process.env.VUE_APP_BASE_API]: ''
        }
      }
    }
  },
  // pwa: {
  //   name: name,
  //   workboxPluginMode: "InjectManifest",
  //   workboxOptions: {
  //     swSrc: path.resolve(__dirname, "src/pwa/service-worker.js")
  //   }
  // },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: [
        path.resolve(__dirname, 'src/styles/_variables.scss'),
        path.resolve(__dirname, 'src/styles/_mixins.scss')
      ]
    }
  },
  chainWebpack (config) {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    config.set('name', name)

    // https://webpack.js.org/configuration/devtool/#development
    // Change development env source map if you want.
    // The default in vue-cli is 'eval-cheap-module-source-map'.
    // config
    //   .when(process.env.NODE_ENV === 'development',
    //     config => config.devtool('eval-cheap-source-map')
    //   )

    if (projectName.name === 'base') {
      config.resolve.alias.set('@', path.resolve('src'))
    } else {
      config.resolve.alias.set('@', path.resolve('src'))
        .set('~', path.resolve("src/projects/" + projectName.name))
    }

    config.when(process.env.NODE_ENV !== 'development', config => {
      config.performance
        .hints("warning") // false | "error" | "warning"。打开/关闭提示
        .maxEntrypointSize(1524288) // 入口起点表示针对指定的入口，对于所有资源，要充分利用初始加载时(initial load time)期间。此选项根据入口起点的最大体积，控制 webpack 何时生成性能提示。默认值是：250000
        .maxAssetSize(1524288) // 资源(asset)是从 webpack 生成的任何文件。此选项根据单个资源体积，控制 webpack 何时生成性能提示。默认值是：250000

      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial' // only package third parties that are initially dependent
          },
          elementUI: {
            name: 'chunk-elementUI', // split elementUI into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
          },
          commons: {
            name: 'chunk-commons',
            test: path.resolve(__dirname, 'src/components'),
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      config.optimization.runtimeChunk('single')
    })
  }
}
